﻿#pragma once


#include <Windows.h>
#include <iostream>
#include <sstream>


std::string GetSysErrorString(DWORD errorCode);
HANDLE CheckHandle(HANDLE hHandle, const char* file, int line);
void CheckHRESULT(HRESULT hr, const char* file, int line);

template<typename T>
T CheckNULL(T t, const char* file, int line) {
	auto ptr = reinterpret_cast<void*>(t);
	if (ptr == nullptr)
	{
		std::ostringstream oss;
		oss << "hHandle is null!" << std::endl;
		oss << "[File]:" << file << std::endl;
		oss << "[Line]:" << line << std::endl;
		int errorCode = GetLastError();
		oss << GetSysErrorString(errorCode) << std::endl;
		throw std::runtime_error(oss.str());
	}
	return t;
}

void Assert(bool flag, const char* msg, const char* file, int line);


// 检查 HANDLE 和 HRESULT
#define MY_CHECK_HRESULT(hr) CheckHRESULT(hr,__FILE__,__LINE__)
#define MY_CHECK_HANDLE(handle) CheckHandle(handle,__FILE__,__LINE__)
#define MY_CHECK_NULL(t) CheckNULL(t,__FILE__,__LINE__)
#define MY_ASSET(flag,msg) Assert(flag,msg,__FILE__,__LINE__)




